package com.bestv.search.common.util;

import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.dataSource.DataSourceKeyUtil;
import com.bestv.search.common.model.Channel;
import com.bestv.search.common.model.Info;
import com.bestv.search.common.model.Schedule;
import com.bestv.search.common.model.Type;
import com.bestv.search.common.model.uid.UID;
import com.bestv.search.common.service.ChannelManager;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * This Class is for import Schdules to DB
 */
public class ScheduleUtil {

    public static List<Info> scheduleTxtFileToList(File file, ChannelManager channelManager, String siteCode)
            throws Exception {
        List<String> list = new ArrayList<String>();
        FileReader fr = new FileReader(file);
        @SuppressWarnings("resource")
        BufferedReader br = new BufferedReader(fr);
        for (String l = br.readLine(); l != null && !"".equals(l); l = br.readLine()) {
            list.add(l);
        }
        return parseTxtSchedule(list, channelManager, siteCode);
    }

    public static List<Info> scheduleExcelFileToList(File file, ChannelManager channelManager, String siteCode)
            throws Exception {
        InputStream is = new FileInputStream(file);
        Workbook wb = WorkbookFactory.create(is);
        Sheet sheet = wb.getSheetAt(0);
        List<Info> schs = new ArrayList<Info>();
        Type t = new Type();
        t.setId(5L);
        t.setName("schedule");
        t.setDescription("节目单");
        String channel = "";
        for (Row row : sheet) {
            if (row.getRowNum() == 0)
                continue;
            channel = row.getCell(0).getStringCellValue();
            DataSourceKey key = DataSourceKeyUtil.convert(siteCode);
            Channel chanObj = channelManager.getChannelByName(key, channel);
            if (chanObj != null) {
                channel = chanObj.getCode();
            } else {
                continue;
            }
            Schedule sch = new Schedule();
            sch.setName(row.getCell(1).getStringCellValue());
            sch.setChannel(channel);
            sch.setType(t);
            sch.setCode(channel);
            sch.setUpdateFlag(0);
            sch.setStatus(1);
            Date now = new Date();
            sch.setUpdateTime(now);
            sch.setUpdateTimeMillis(now.getTime());
            sch.setUpdateTimeForIndex(now);
            sch.setStartPlayTime(DateUtil.convertStringToDate("yyyy.MM.dd HH:mm:ss", row.getCell(2)
                    .getStringCellValue()));
            sch.setEndPlayTime(DateUtil.convertStringToDate("yyyy.MM.dd HH:mm:ss", row.getCell(3).getStringCellValue()));
            sch.setId(new Long(UID.getUID(sch)));
            schs.add(sch);
        }
        return schs;
    }

    private static List<Info> parseTxtSchedule(List<String> list, ChannelManager channelManager, String siteCode)
            throws Exception {
        Type t = new Type();
        t.setId(5L);
        t.setName("schedule");
        t.setDescription("节目单");
        String channel = "";
        String date = "";
        List<Info> schs = new ArrayList<Info>();
        boolean ignoreScheduleCausChannelNotExist = false;
        for (String s : list) {
            if (s.startsWith("Channel")) {
                channel = s.substring(s.indexOf(":") + 1);
                DataSourceKey key = DataSourceKeyUtil.convert(siteCode);
                Channel chanObj = channelManager.getChannelByName(key, channel);
                if (chanObj != null) {
                    channel = chanObj.getCode();
                    ignoreScheduleCausChannelNotExist = false;
                } else {
                    ignoreScheduleCausChannelNotExist = true;
                }
                continue;
            }
            if (s.startsWith("Date")) {
                date = s.substring(s.indexOf(":") + 1);
                continue;
            }
            if (ignoreScheduleCausChannelNotExist)
                continue;
            String time = s.substring(0, s.indexOf("|"));
            String start = time.substring(0, time.indexOf("-"));
            String end = time.substring(time.indexOf("-") + 1);
            String name = s.substring(s.indexOf("|") + 1);

            Schedule sch = new Schedule();
            sch.setName(name);
            sch.setChannel(channel);
            sch.setType(t);
            sch.setCode(channel);
            sch.setUpdateFlag(0);
            Date now = new Date();
            sch.setUpdateTime(now);
            sch.setUpdateTimeMillis(now.getTime());
            sch.setUpdateTimeForIndex(now);
            sch.setStartPlayTime(DateUtil.convertStringToDate("yyyyMMddHHmm", date + start));
            sch.setEndPlayTime(DateUtil.convertStringToDate("yyyyMMddHHmm", date + end));
            sch.setId(new Long(UID.getUID(sch)));
            schs.add(sch);
        }
        return schs;
    }

    public static void main(String[] args) throws Exception {
        ScheduleUtil.scheduleExcelFileToList(new File("D:\\work\\temp\\program.xls"), null, null);
    }
}
